﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class Frmzyxxcx : Form
    {
        public Frmzyxxcx()
        {
            InitializeComponent();
        }
        public Frmzyxxcx(string flag)
        {
            InitializeComponent();
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if (flag == "del")
            {//删除按钮显示
                this.btnDel.Visible = true;
            }
        }
         public void bindDataGrid()
        {
            string id = this.txtId.Text.Trim();
            string name = this.TxtName.Text.Trim();
            SpecialtyInfoData data = new SpecialtyInfoData();
            data.Specialtyid = id;
            data.Specialtymc = name;
            try
            {
                DataSet ds = SpecialtyOperation.getSpecialty(data);
                this.dataGridView1.DataSource = ds.Tables[0];
            }
             catch(Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            bindDataGrid();
        }
        private void btnMod_Click(object sender, EventArgs e)
        {
           
            if (this.dataGridView1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGridView1.CurrentRow.Index;
            if (index < 0)
            {//必须选择一条要修改的记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                string id = this.txtId.Text.Trim();
                string name = this.TxtName.Text.Trim();
                SpecialtyInfoData data = new SpecialtyInfoData();
                data.Specialtyid = id;
                data.Specialtymc = name;
                DataSet ds = SpecialtyOperation.getSpecialty(data);
                Frmaddzyxx modzyxx = new Frmaddzyxx(ds.Tables[0].Rows[index]["专业id"].ToString());
                modzyxx.ShowDialog(this);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int index = this.dataGridView1.CurrentRow.Index;
            
            if (index < 0)
            {//必须选择一条删除的记录
                MessageBox.Show("请选择要删除的记录!", "提示");
                return;
            }
            else
            {
                string id = this.txtId.Text.Trim();
                string name = this.TxtName.Text.Trim();
                SpecialtyInfoData data = new SpecialtyInfoData();
                data.Specialtyid = id;
                data.Specialtymc = name;
                DataSet ds = SpecialtyOperation.getSpecialty(data);
                this.dataGridView1.DataSource = ds.Tables[0];
                if(MessageBox.Show("确认要删除吗？","删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    try
                    {
                        if (SpecialtyOperation.deleteSpecialty(ds.Tables[0].Rows[index]["专业id"].ToString()))
                        {
                            MessageBox.Show("删除成功!", "提示");
                            bindDataGrid();
                        }
                        else
                        {
                            MessageBox.Show("删除失败!","错误");
                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！","错误");
                    }
                }
            }
        }

    }
    //SpecialtyInfoData
    public class SpecialtyInfoData
    {
        private string specialtyid = "";
        private string specialtymc = "";
        public string Specialtyid//专业代号
        {
            get { return specialtyid; }
            set { specialtyid = value; }
        }
        public string Specialtymc//专业名称
        {
            get { return specialtymc; }
            set { specialtymc = value; }
        }
    }
    //SpecialtyOperation
    public class SpecialtyOperation
    {
        private static DataAccess dataAccess = new DataAccess();
        public static bool insertSpecialty(SpecialtyInfoData specialtyInfoData)
        {//数据插入
            string sql = String.Format("insert into 专业信息表(专业id,专业名称)values('{0}','{1}')",specialtyInfoData.Specialtyid,specialtyInfoData.Specialtymc);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateSpecialty(SpecialtyInfoData specialtyInfoData)
        {//数据修改
            string sql = String.Format("update 专业信息表 set 专业名称='{1}' where 专业id='{0}'", specialtyInfoData.Specialtyid, specialtyInfoData.Specialtymc);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteSpecialty(string specialtyid)
        {//数据删除
            string sql = String.Format("delete 专业信息表 where 专业id='{0}'", specialtyid);
            return dataAccess.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getSpecialty(SpecialtyInfoData specialtyInfoData)
        {//数据查询
            string condition = "";//查询的条件
            if (specialtyInfoData.Specialtyid != null && "" != specialtyInfoData.Specialtyid)
            {
                condition += "and 专业id='" + specialtyInfoData.Specialtyid + "'";
            }
            if(specialtyInfoData.Specialtymc!=null&&""!=specialtyInfoData.Specialtymc)
            {
                condition += " and 专业名称 like '%" + specialtyInfoData.Specialtymc + "%'";
            }
            string sql = "select 专业id,专业名称 from 专业信息表 where 1=1" + condition;
            return dataAccess.GetDataSet(sql, "专业信息表");//执行sql语句
        }
       
    }
   
}